package com.org.gsc.shuaxindemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qundui on 2017/6/24.
 */

public class TestAct extends Activity {
    private PullToRefreshListView mPullToRefreshListView;
    MyAdapter mMyAdapter=new MyAdapter();
    List<String> madas=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        mPullToRefreshListView= (PullToRefreshListView) findViewById(R.id.lv);
        mPullToRefreshListView.setAdapter(mMyAdapter);
        mPullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
         for(int i=0;i<20;i++){
             madas.add("第"+(i+1)+"条数目");
         }
        mPullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                for(int i=0;i<20;i++){
                    madas.add("onPullDownToRefresh第"+(i+1)+"条数目");
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mMyAdapter.notifyDataSetChanged();
                        mPullToRefreshListView.onRefreshComplete();
                    }
                },2000);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                for(int i=0;i<20;i++){
                    madas.add("onPullUpToRefresh第"+(i+1)+"条数目");
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mMyAdapter.notifyDataSetChanged();
                        mPullToRefreshListView.onRefreshComplete();
                    }
                },2000);
            }
        });
    }

    class  MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return madas.size()==0?0:madas.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView=new TextView(TestAct.this);
            textView.setText(madas.get(position));
            return textView;
        }
    }
}
